package com.baichang.taoshunda.controller.mobile.business;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.UserBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(ControllerConstants.BUSSINESSBANKCARD)
public class BussinessBankCardMController extends BaseMobileController {

    @Autowired
    private UserBankService userBankService;

    /**
     * 查看商家的银行卡信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findBusinessBankCard")
    public void findBusinessBankCard(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                return userBankService.findBusinessBankCard(bussId);
            }
        });
    }

    /**
     * 修改商家的银行卡信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateBusinessBankCard")
    public void updateBusinessBankCard(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bankId = getInt(json, "bankId", 0);
                Integer bankTypeId = getInt(json, "bankTypeId", 0);
                String bankNo = getString(json, "bankNo", "");
                return userBankService.updateBusinessBankCard(bankId, bankTypeId, bankNo);
            }
        });
    }

    /**
     * 解绑银行卡
     *
     * @param request
     * @param response
     */
    @RequestMapping("/unBundling")
    public void unBundling(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                Integer bankId = getInt(json, "bankId", 0);
                return userBankService.unBundling(bussId, bankId);
            }
        });
    }

    /**
     * 查看银行信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findBank")
    public void findBank(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                return userBankService.findBank();
            }
        });
    }

    /**
     * 绑定银行卡
     *
     * @param request
     * @param response
     */
    @RequestMapping("/bussinessBundling")
    public void bussinessBundling(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                Integer cardTypeId = getInt(json, "cardTypeId", 0);
                String cardNumber = getString(json, "cardNumber", "");
                String userName = getString(json, "name", "");
                return userBankService.bussinessBundling(bussId, cardTypeId, cardNumber, userName);
            }
        });
    }

    /**
     * 根据银行卡号检查是否绑定过银行卡
     *
     * @param request
     * @param response
     */
    @RequestMapping("/checkBundling")
    public void checkBundling(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                Integer cardTypeId = getInt(json, "cardTypeId", 0);
                String cardNumber = getString(json, "cardNumber", "");
                return userBankService.checkBundling(bussId, cardTypeId, cardNumber);
            }
        });
    }

}
